package com.cq.oa.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oa.base.session.SessionManger;
import oa.base.util.DateUtil;
import oa.base.web.Action.BaseAction;

import com.cq.oa.domain.sm.Res;
import com.cq.oa.domain.sm.User;
import com.cq.oa.domain.utils.Resources;
import com.cq.oa.service.sm.ResService;

public class MainAction extends BaseAction<User>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4317077916829177380L;

	@Override
	protected Class<User> getEntityClass() {
		return User.class;
	}
	
	private ResService resService;
	
	
	public void setResService(ResService resService) {
		this.resService = resService;
	}

	private List<Res> findByResMenu(){
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("orgSid", 1L);
		params.put("status", 1);
		Map<String,Object> sortMap = new HashMap<String,Object>();
		sortMap.put("orderBy", "asc");
		return resService.findParByList(params, null, sortMap);
	}
	
	/***
	 * 构建菜单
	 * @return
	 * lilang 
	 * 2013-6-22下午8:33:12
	 */
	private List<Resources> bulidResourceList(){
		List<Res> resList = this.findByResMenu();
		return this.commonBulidList(0L, resList);
	}
	/**
	 *  递归菜单
	 */
	private List<Resources> commonBulidList(Long upId,List<Res> resList){
		List<Resources> resources = new ArrayList<Resources>();
		for(Res res:resList){
			if(upId.equals(res.getPid())){
				Resources resource = new Resources();
				resource.setSid(res.getSid());
				resource.setLink(res.getLink());
				resource.setCname(res.getCname());
				resource.setCode(res.getCode());
				resource.setIsLeaf(res.getIsLeaf());
				//0 目录  1菜单
				if(0==res.getIsLeaf()){
					resource.setResList(commonBulidList(res.getSid(), resList));
				}
				resources.add(resource);
			}
		}
		return resources;
	}
	
	
	
	/**
	 * 进入首页
	 * @return
	 */
	public String main(){
		this.putKey.put("count", SessionManger.getCount());
		this.putKey.put("menuList", bulidResourceList());
		this.putKey.put("showWeek", DateUtil.showWeekTime(new Date()));
		return SUCCESS;
	}

}
